home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.applet.AudioClip;
- import java.awt.Button;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.GridLayout;
- import java.awt.Image;
- import java.awt.Label;
- import java.awt.MediaTracker;
- import java.awt.Panel;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.InputEvent;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.net.URL;
- import java.util.EventObject;
-
- public class TicTacToe extends Applet implements MouseListener, ActionListener {
- // $FF: renamed from: wi int
- int field_0;
- // $FF: renamed from: he int
- int field_1;
- Color backColor;
- final Color borderColorDef;
- Color borderColor;
- final int nCases;
- Image buffer;
- // $FF: renamed from: b java.awt.Graphics
- Graphics field_2;
- Image crossIm;
- Image circleIm;
- int[][] mat;
- int cote;
- boolean loaded;
- int nCoups;
- int first;
- int mode;
- boolean resetable;
- boolean textable;
- String text;
- // $FF: renamed from: p java.awt.Panel
- Panel field_3;
- boolean playable;
- final String memInit;
- private String mem;
- private final int[][][] rep;
- private AudioClip clicSound;
- private AudioClip computerSound;
- private AudioClip playerSound;
- private AudioClip drawSound;
-
- public String getAppletInfo() {
- return "Name: TicTacToe\r\nAuthor: Taiji Software\r\n";
- }
-
- public TicTacToe() {
- this.backColor = Color.white;
- this.borderColorDef = Color.black;
- this.nCases = 3;
- this.memInit = "000 000 000 000 000 000 000 000";
- this.mem = "000 000 000 000 000 000 000 000";
- this.rep = new int[][][]{{{0, 12, 24}, {1, 16}, {2, 20, 30}}, {{4, 13}, {5, 17, 25, 29}, {6, 21}}, {{8, 14, 28}, {9, 18}, {10, 22, 26}}};
- this.mode = 1;
- this.first = 0;
- this.borderColor = this.borderColorDef;
- ((Component)this).addMouseListener(this);
- }
-
- public void register() {
- try {
- URL u = new URL("http://www.taijisoftware.com");
- ((Applet)this).getAppletContext().showDocument(u, "_blank");
- ((Applet)this).stop();
- } catch (Exception e) {
- System.out.println(e);
- ((Applet)this).stop();
- }
- }
-
- public void init() {
- String codeBase = null;
-
- try {
- codeBase = (new URL(((Applet)this).getCodeBase().toString())).getHost();
- System.out.println("Copyright 1999, 2001 Taiji Software(tm)\nYour domain name is : " + codeBase);
- codeBase = codeBase.toUpperCase();
- } catch (Exception var10) {
- }
-
- if (!((Applet)this).getCodeBase().toString().toUpperCase().startsWith("FILE") || ((Applet)this).getParameter("debug") != null) {
- String regCode = ((Applet)this).getParameter("registration_code");
- if (regCode == null) {
- regCode = ((Applet)this).getParameter("reg_domain");
- if (regCode == null) {
- this.register();
- } else {
- if (regCode.length() == codeBase.length() + 4 && !codeBase.startsWith("WWW.")) {
- codeBase = "WWW." + codeBase;
- } else if (regCode.length() == codeBase.length() - 4 && codeBase.startsWith("WWW.")) {
- codeBase = codeBase.substring(4);
- }
-
- char[] chars = new char[codeBase.length()];
- codeBase.getChars(0, codeBase.length(), chars, 0);
- String key = new String("haricot");
- char[] chars2 = new char[key.length()];
- key.getChars(0, key.length(), chars2, 0);
-
- for(int i = 0; i < codeBase.length(); ++i) {
- int j;
- if (i >= key.length()) {
- j = i - key.length() * (i / key.length());
- } else {
- j = i;
- }
-
- chars[i] += chars2[j];
- chars[i] = (char)(chars[i] - chars[i] / 26 * 26 + 97);
- }
-
- String res = new String(chars);
- if (!res.equalsIgnoreCase(regCode)) {
- this.register();
- }
- }
- } else if (!regCode.equals("settevercsedegnamiaj") && !regCode.equals("8078")) {
- this.register();
- }
- }
-
- this.getParameters();
- this.crossIm = ((Applet)this).getImage(((Applet)this).getCodeBase(), ((Applet)this).getParameter("cross"));
- this.circleIm = ((Applet)this).getImage(((Applet)this).getCodeBase(), ((Applet)this).getParameter("circle"));
- MediaTracker tracker = new MediaTracker(this);
- tracker.addImage(this.crossIm, 0);
- tracker.addImage(this.circleIm, 0);
-
- try {
- tracker.waitForAll();
- this.loaded = !tracker.isErrorAny();
- } catch (Exception e) {
- System.err.println(e);
- }
-
- if (!this.loaded) {
- ((Applet)this).stop();
- }
-
- this.cote = (this.field_1 - 3 - 1) / 3;
- this.buffer = ((Component)this).createImage(this.field_0, this.field_1);
- this.field_2 = this.buffer.getGraphics();
- this.playable = false;
- this.mat = new int[3][3];
- if (this.first == 0) {
- this.askFirst();
- } else if (this.first == 2) {
- this.computerPlays();
- ++this.nCoups;
- this.playable = true;
- } else {
- this.playable = true;
- }
-
- ((Component)this).repaint();
- }
-
- public void getParameters() {
- this.field_0 = ((Component)this).getSize().width;
- this.field_1 = ((Component)this).getSize().height;
- String s = ((Applet)this).getParameter("mode");
- if (s != null) {
- this.mode = Integer.parseInt(s);
- }
-
- s = ((Applet)this).getParameter("first");
- if (s != null) {
- this.first = Integer.parseInt(s);
- }
-
- this.backColor = this.getColor("background_color");
- ((Component)this).setBackground(this.backColor);
- this.borderColor = this.getColor("border_color");
- s = ((Applet)this).getParameter("clic_sound_name");
- if (s != null) {
- this.clicSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
- }
-
- s = ((Applet)this).getParameter("computer_sound_name");
- if (s != null) {
- this.computerSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
- }
-
- s = ((Applet)this).getParameter("player_sound_name");
- if (s != null) {
- this.playerSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
- }
-
- s = ((Applet)this).getParameter("draw_sound_name");
- if (s != null) {
- this.drawSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
- }
-
- }
-
- public Color getColor(String param) {
- String s = ((Applet)this).getParameter(param);
- if (s != null) {
- if (s.substring(0, 1).equals("#")) {
- s = s.substring(1);
- int i = Integer.parseInt(s, 16);
- return new Color(i);
- } else {
- return null;
- }
- } else {
- return null;
- }
- }
-
- public void askFirst() {
- this.field_3 = new Panel();
- this.field_3.setLayout(new GridLayout(2, 2));
- Button b1 = new Button("player");
- b1.addActionListener(this);
- Button b2 = new Button("computer");
- b2.addActionListener(this);
- this.field_3.add(new Label("First"));
- this.field_3.add(new Label("player ?"));
- this.field_3.add(b1);
- this.field_3.add(b2);
- ((Container)this).add("Center", this.field_3);
- }
-
- public void actionPerformed(ActionEvent event) {
- if (((EventObject)event).getSource() instanceof Button) {
- Button b = (Button)((EventObject)event).getSource();
- ((Container)this).remove(this.field_3);
- if (b.getLabel().equals("computer")) {
- this.computerPlays();
- ++this.nCoups;
- }
-
- ((Component)this).repaint();
- this.playable = true;
- }
-
- }
-
- public final void paint(Graphics g) {
- if (this.loaded) {
- this.paintGrid(this.field_2);
- this.paintCases(this.field_2);
- if (this.textable) {
- this.paintText(this.field_2);
- }
-
- g.drawImage(this.buffer, 0, 0, this);
- }
- }
-
- public final void update(Graphics g) {
- this.paint(g);
- }
-
- public final void paintGrid(Graphics b) {
- for(int i = 0; i <= 3; ++i) {
- b.setColor(this.borderColor);
- b.drawLine(i * (this.cote + 1), 0, i * (this.cote + 1), 3 * (this.cote + 1));
- b.drawLine(0, i * (this.cote + 1), 3 * (this.cote + 1), i * (this.cote + 1));
- }
-
- }
-
- public final void paintCases(Graphics b) {
- for(int i = 1; i <= 3; ++i) {
- for(int j = 1; j <= 3; ++j) {
- if (this.mat[i - 1][j - 1] == 1) {
- b.drawImage(this.crossIm, (j - 1) * (this.cote + 1) + 1, (i - 1) * (this.cote + 1) + 1, this.cote, this.cote, this);
- } else if (this.mat[i - 1][j - 1] == 5) {
- b.drawImage(this.circleIm, (j - 1) * (this.cote + 1) + 1, (i - 1) * (this.cote + 1) + 1, this.cote, this.cote, this);
- }
- }
- }
-
- }
-
- public final void paintText(Graphics b) {
- int h = this.cote / 3;
- b.setFont(new Font("TimesRoman", 1, h));
- b.setColor(this.borderColor);
- FontMetrics fm = b.getFontMetrics();
- int w = fm.stringWidth(this.text);
- b.drawString(this.text, (this.field_0 - w) / 2, this.field_1 / 2);
- this.textable = false;
- }
-
- public void reset() {
- for(int i = 1; i <= 3; ++i) {
- for(int j = 1; j <= 3; ++j) {
- this.mat[i - 1][j - 1] = 0;
- }
- }
-
- this.nCoups = 0;
- this.mem = "000 000 000 000 000 000 000 000";
- this.field_2.clearRect(0, 0, this.field_0, this.field_1);
- ((Component)this).repaint();
- if (this.first == 0) {
- this.playable = false;
- ((Container)this).add("Center", this.field_3);
- }
-
- }
-
- public final boolean occupe(int i, int j) {
- return !this.mem.substring(this.rep[i - 1][j - 1][0], this.rep[i - 1][j - 1][0] + 1).equals("0");
- }
-
- public final void rempli(int i, int j, String s) {
- for(int k = 0; k < this.rep[i - 1][j - 1].length; ++k) {
- String s1 = this.mem.substring(0, this.rep[i - 1][j - 1][k]);
- String s2 = this.mem.substring(this.rep[i - 1][j - 1][k] + 1);
- this.mem = s1 + s + s2;
- this.mat[i - 1][j - 1] = Integer.parseInt(s);
- }
-
- }
-
- public final int[] donneIJ(int f) {
- for(int i = 0; i < 3; ++i) {
- for(int j = 0; j < 3; ++j) {
- for(int k = 0; k < this.rep[i][j].length; ++k) {
- if (this.rep[i][j][k] == f) {
- int[] ind = new int[]{i + 1, j + 1};
- return ind;
- }
- }
- }
- }
-
- return null;
- }
-
- public final boolean resolution() {
- if (this.mem.indexOf("111") != -1) {
- if (this.playerSound != null) {
- this.playerSound.play();
- }
-
- this.text = "player wins !";
- this.textable = true;
- this.resetable = true;
- return true;
- } else if (this.mem.indexOf("555") != -1) {
- if (this.computerSound != null) {
- this.computerSound.play();
- }
-
- this.text = "computer wins !";
- this.textable = true;
- this.resetable = true;
- return true;
- } else {
- return false;
- }
- }
-
- public final void computerPlays() {
- if (this.mode == 1) {
- String[] cod = new String[]{"550", "055", "505", "110", "101", "011"};
-
- for(int i = 0; i < cod.length; ++i) {
- int f = this.mem.indexOf(cod[i]);
- if (f != -1) {
- f = this.mem.indexOf("0", f);
- int[] ind = this.donneIJ(f);
- this.rempli(ind[0], ind[1], "5");
- ((Component)this).repaint();
- return;
- }
- }
-
- if (!this.occupe(2, 2)) {
- this.rempli(2, 2, "5");
- ((Component)this).repaint();
- return;
- }
-
- if (this.mat[1][1] == 1) {
- if (!this.occupe(3, 1)) {
- this.rempli(3, 1, "5");
- ((Component)this).repaint();
- return;
- }
-
- if (!this.occupe(3, 3)) {
- this.rempli(3, 3, "5");
- ((Component)this).repaint();
- return;
- }
-
- if (!this.occupe(1, 1)) {
- this.rempli(1, 1, "5");
- ((Component)this).repaint();
- return;
- }
- } else if (this.mat[1][1] == 5) {
- if (this.occupe(3, 1) && !this.occupe(1, 3)) {
- this.rempli(1, 3, "5");
- ((Component)this).repaint();
- return;
- }
-
- if (this.occupe(3, 3) && !this.occupe(1, 1)) {
- this.rempli(1, 1, "5");
- ((Component)this).repaint();
- return;
- }
-
- if (this.occupe(1, 3) && !this.occupe(3, 1)) {
- this.rempli(3, 1, "5");
- ((Component)this).repaint();
- return;
- }
-
- if (this.occupe(1, 1) && !this.occupe(3, 3)) {
- this.rempli(3, 3, "5");
- ((Component)this).repaint();
- return;
- }
-
- if (this.occupe(3, 2) && !this.occupe(1, 3)) {
- this.rempli(1, 3, "5");
- ((Component)this).repaint();
- return;
- }
-
- if (this.occupe(1, 2) && !this.occupe(3, 3)) {
- this.rempli(3, 3, "5");
- ((Component)this).repaint();
- return;
- }
-
- if (this.occupe(2, 1) && !this.occupe(1, 3)) {
- this.rempli(1, 3, "5");
- ((Component)this).repaint();
- return;
- }
-
- if (this.occupe(2, 3) && !this.occupe(3, 1)) {
- this.rempli(3, 1, "5");
- ((Component)this).repaint();
- return;
- }
- }
-
- String[] cod2 = new String[]{"005", "050", "500"};
-
- for(int i = 0; i < cod2.length; ++i) {
- int f = this.mem.indexOf(cod2[i]);
- if (f != -1) {
- f = this.mem.indexOf("0", f);
- int[] ind = this.donneIJ(f);
- this.rempli(ind[0], ind[1], "5");
- ((Component)this).repaint();
- return;
- }
- }
- }
-
- int i;
- int j;
- do {
- int r = (int)((double)9.0F * Math.random());
- i = r / 3 + 1;
- j = r % 3 + 1;
- } while(this.occupe(i, j));
-
- this.rempli(i, j, "5");
- ((Component)this).repaint();
- }
-
- public void draw() {
- this.text = "draw !";
- this.textable = true;
- ((Component)this).repaint();
- this.resetable = true;
- }
-
- public void mouseClicked(MouseEvent e) {
- }
-
- public void mouseEntered(MouseEvent e) {
- }
-
- public void mouseExited(MouseEvent e) {
- }
-
- public void mousePressed(MouseEvent e) {
- if (this.playable) {
- if (this.resetable) {
- this.reset();
- this.resetable = false;
- } else if (((InputEvent)e).isMetaDown()) {
- this.reset();
- } else {
- int x = e.getX();
- int y = e.getY();
- int j = (x - 1) / (this.cote + 1) + 1;
- int i = (y - 1) / (this.cote + 1) + 1;
- if (!this.occupe(i, j)) {
- if (this.clicSound != null) {
- this.clicSound.play();
- }
-
- this.rempli(i, j, "1");
- ((Component)this).repaint();
- if (this.resolution()) {
- return;
- }
-
- ++this.nCoups;
- if (this.nCoups == 9) {
- this.draw();
- if (this.drawSound != null) {
- this.drawSound.play();
- }
-
- return;
- }
-
- this.computerPlays();
- if (this.resolution()) {
- return;
- }
-
- ++this.nCoups;
- if (this.nCoups == 9) {
- this.draw();
- if (this.drawSound != null) {
- this.drawSound.play();
- }
-
- return;
- }
- }
-
- }
- }
- }
-
- public void mouseReleased(MouseEvent e) {
- }
- }
-